package demo1;

/**
 * Created with IntelliJ IDEA.
 * Description:链式队列
 * User: LianBao
 * Date: 2024-04-01
 * Time: 14:25
 */
public class myQueue<E> {

    static class ListNode {
        public ListNode prev;//前驱
        public Object val;//数据
        public ListNode next;//后继

        public ListNode(Object val) {
            this.val = val;
        }
    }

    public ListNode head;//队头
    public ListNode tail;//队尾

    //入队(尾插)
    public E offer(E val) {
        ListNode newNode = new ListNode(val);
        if (tail == null) {
            head = tail = newNode;
        } else {
            tail.next = newNode;
            newNode.prev = tail;
        }
        return val;
    }

    //出队(头删)
    public E poll() {
        if (head == null) {
            return null;
        }
        Object ret = head.val;
        head = head.next;
        head.prev = null;
        return (E) ret;
    }

    //获取队头
    public E peek() {

        if (head == null) {
            return null;
        }
        return (E) head.val;
    }

}
